package com.taxi.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.taxi.config.AdminConfiguration;
import com.taxi.constant.IdentityConstant;
import com.taxi.constant.TokenConstants;
import com.taxi.request.AdminUser;
import com.taxi.response.AdminTokenResponse;
import com.taxi.result.Result;
import com.taxi.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminUserService {
    @Autowired
    private AdminConfiguration adminConfiguration;
    public Result login(AdminUser user){
        String account = adminConfiguration.getUsername();
        String password = adminConfiguration.getPassword();
        if(!account.equals(user.getUsername()) || !password.equals(user.getPassword())){
            return Result.fail().message("账号或密码错误！");
        }
        String token = JwtUtils.generatorToken(account, IdentityConstant.ADMIN_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        AdminTokenResponse adminTokenResponse = new AdminTokenResponse();
        adminTokenResponse.setToken(token);
        return Result.ok(adminTokenResponse);
    }

    public Result getInfo(String token){
        String username = JwtUtils.parseToken(token).getPhone();
        JSONObject userInfo = new JSONObject();
        JSONArray roles = new JSONArray();
        roles.add(username);
        userInfo.put("roles", roles);
        userInfo.put("introduction", "I am a super administrator");
        userInfo.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        userInfo.put("name", "Super Admin");
        return Result.ok(userInfo);
    }



}
